#include "login.h"
#include "ui_login.h"
#include <QWidget>
#include "admininfo.h"
#include "teacherinfo.h"
#include "studentinfo.h"
#include <QDebug>
#include <QPainter>
#include <QMovie>
#include <QHBoxLayout>
#include <QCursor>
#include <QMessageBox>
#include <QString>
#include <QSqlQuery>
#include <QSqlRecord>
#include "forgrt.h"

Login::Login(QWidget *parent) :
    QWidget(parent),ui(new Ui::Login)
{
    ui->setupUi(this);
    initWindow();
}

Login::~Login()
{
    delete ui;
}
void Login::slotsenddata()
{
    w;
    w->data = actor ;
}
void Login::showAdminW()
{
    QString A = ui->comboBox_bianhao->currentText();
    m_adminW.show();
    m_adminW.selectBoxSlot(A);
}

void  Login::showStudentW()
{
      QString S = ui->comboBox_bianhao->currentText();
    m_studentW.show();
    m_studentW.selectBoxSlot(S);
}

void  Login::showTeacherW()
{
    QString T = ui->comboBox_bianhao->currentText();
    m_teacherW.show();
    m_teacherW.selectBoxSlot(T);
}

void Login::initWindow()
{
    ui->comboBox_bianhao->setEditable(true);
    QLineEdit* lineEdit = ui->comboBox_bianhao->lineEdit();
    lineEdit->setPlaceholderText(tr("编号"));
    ui->lineEdit_pwd->setPlaceholderText(tr("密码"));
}

void Login::on_pb_tealongin_clicked()
{
    QString id = ui->comboBox_bianhao->currentText();
    ExecSQL exe;
     infoT = exe.selectTeacherInfo(id);
    table_pwd = infoT.getpswd();
    actor =2;
    infoT.display();
}

void Login::on_pb_stulongin_clicked()
{
    QString id = ui->comboBox_bianhao->currentText();
    ExecSQL exes;
    infoS = exes.selectStudentInfo(id);
    table_pwd = infoS.getpswd();
    actor = 3;
    infoS.display();
}

void Login::on_pb_facelongin_clicked()
{
//    ui->lineEdit_pwd->hide();
    active = 0;
    prologin.startDetached("D:\\openmv\\bin\\openmvide.exe",QStringList());
    qDebug()<<"0";
}

void Login::on_pb_peologin_clicked()
{
    QString id = ui->comboBox_bianhao->currentText();
    qDebug()<<id;
    ExecSQL exec;
    infoA = exec.selectAdminInfo(id);
    table_pwd = infoA.getPswd();
    qDebug()<<table_pwd;
    actor = 1;
    infoA.display();
}

void Login::on_pb_pwdlogin_clicked()
{
//    ui->textEdit_face->hide();
     active = 1;
     qDebug()<<"1";
}

void Login::on_pb_login_clicked()
{
    if(active == 1)
    {
        QString pwd = ui->lineEdit_pwd->text();
        if(pwd == table_pwd)
        {
            switch(actor)
            {
            case 1:
                emit signalAdminLoginSuccess(infoA);
                infoA.display();
                showAdminW();
                break;
            case 2:
                emit signalTeacherLoginSuccess(infoT);
                infoT.display();
                showTeacherW();
                 break;
            case 3:
                qDebug()<<"not emit!!";
                emit signalStudentLoginSuccess(infoS);
                infoS.display();
                showStudentW();
                 break;
           default:
                break;
            }
            this->hide();
            qDebug()<<"success: find people!!!";
        }else{
            QMessageBox msgBox;
            msgBox.setText("Error: Your ID or Password !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
              case QMessageBox::Retry:
                  ui->comboBox_bianhao->setFocus();
                  break;
              case QMessageBox::Cancel:
                  this->close();
                  break;
              default:
                  // should never be reached
                  break;
            qDebug()<<"fail : find people!!";
            }
        }
    }else if(active == 0)
    {
//人脸识别
        if(prologin.isOpen() == false)
        {
            prologin.close();
            switch(actor)
            {
            case 1:
                emit signalAdminLoginSuccess(infoA);
                infoA.display();
                showAdminW();
                break;
            case 2:
                emit signalTeacherLoginSuccess(infoT);
                infoT.display();
                showTeacherW();
                 break;
            case 3:
                qDebug()<<"not emit!!";
                emit signalStudentLoginSuccess(infoS);
                infoS.display();
                showStudentW();
                 break;
           default:
                break;
            }
            this->close();
            qDebug()<<"success: find people!!!";
        }else{
            QMessageBox msgBox;
            msgBox.setText("Error: Your ID or Password !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
              case QMessageBox::Retry:
                  ui->comboBox_bianhao->setFocus();
                  break;
              case QMessageBox::Cancel:
                  this->close();
                  break;
              default:
                  // should never be reached
                  break;
            qDebug()<<"fail : find people!!";
            }
        }
    }
}

void Login::on_pb_forgetpwd_clicked()
{
    w = new Forgrt;
    connect(w,&Forgrt::signalForgrt,this,&Login::slotsenddata);
    this->hide();
    w->showForgrt();
}
